package uk.co.threekelvin.perihelion.graphics;

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

import javax.swing.JFrame;

import uk.co.threekelvin.perihelion.engine.World;

public class Render extends Canvas {
	private static final long serialVersionUID = 1L;

	public int xOffset, yOffset;

	private int width, height;

	private JFrame frame;
	private BufferedImage image;
	private int[] pixels;
	private BufferStrategy bs;
	private Graphics g;

	public Render(int width, int height, int scale) {
		this.width = width;
		this.height = height;

		xOffset = 0;
		yOffset = 0;

		Dimension size = new Dimension(width * scale, height * scale);
		setPreferredSize(size);

		image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();

		frame = new JFrame();
		frame.setResizable(false);
		frame.add(this);
		frame.pack();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);

		createBufferStrategy(3);
		bs = getBufferStrategy();
	}

	public void setTitle(String title) {
		frame.setTitle(title);
	}

	public void render(World world) {
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				pixels[x + y * width] = 0x000000;
			}
		}

		draw();
	}

	private void draw() {
		g = bs.getDrawGraphics();
		g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
		bs.show();
		g.dispose();
	}

}
